﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CoDbObject;
using SZJT.Utility.DataSource;

namespace Colligate.DataSource.DriverTraining.Sys.Organization
{
    class DataNode_SysOrganizationSchool:BaseDataTree
    {
        private const string DATATYPE_SCHOOL_INFO = "SysOrgSchoolInfo";
        private const string DATATYPE_SCHOOL_QUERY = "SysOrgSchoolQuery";

        public DataNode_SysOrganizationSchool(string taskGuid, IDataSourceLayer dataLayer, IDataService dataService)
            : base(taskGuid, dataLayer, dataService)
        {
            _dataTypeList[DataTypeCategory.SET_DATA_LIST] = new string[] { DATATYPE_SCHOOL_INFO };
            _dataTypeList[DataTypeCategory.GET_DATA_LIST] = new string[] { DATATYPE_SCHOOL_INFO };
            _dataTypeList[DataTypeCategory.TRANSFORM_LIST] = new string[] { DATATYPE_SCHOOL_QUERY };
            _dataDefine = new SystemOrgSchool(dataLayer);
        }

        #region Public Method

        public override int SetData(OUDataInfo userInfo, string dataGuid, string dataType, System.Xml.XmlNode xmlData)
        {
            int result = 1001;
            switch (dataType)
            {
                case DATATYPE_SCHOOL_INFO:
                    result = SetSchoolInfo(userInfo, dataGuid, xmlData);
                    break;
            }
            return result;
        }

        public override XmlNode GetData(OUDataInfo UserInfo, string DataGuid, string DataType)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_SCHOOL_INFO:
                    ResultInfo = GetSchoolInfo(UserInfo, DataGuid);
                    break;
            }

            return ResultInfo;
        }

        public override XmlNode TransformData(OUDataInfo UserInfo, string DataType, XmlNode XmlParam)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_SCHOOL_QUERY:
                    ResultInfo = GetSchoolList(UserInfo, XmlParam);
                    break;
            }

            return ResultInfo;
        }

        #endregion


        #region 驾校元数据
        //<!-- 应用标识：智慧驾培学事管理系统 -->
        //<!-- 模块分类：驾校设置 -->
        //<!-- 功能分类：组织机构 -->
        //<!-- 分类序号：1011 -->
        //<!-- 数据编号：ZHJP-SYSTEM-ORG-1011 -->
        //<!-- 数据类型：元数据 -->
        //<!-- 数据名称：驾校信息 -->
        //<!-- 数据代码：SysOrgCompanyInfo -->
        //<!-- 数据说明：驾校的基本信息 -->
        //<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{驾校信息标识}" DataType="SysOrgCompanyInfo">
        //    <SchoolGuid Type="GUID" Mandatory="是">{驾校信息标识}</SchoolGuid>
        //    <SchoolName Type="TEXT" Mandatory="是" Length="50">{驾校名称，简称}</SchoolName>
        //    <SchoolFullName Type="TEXT" Mandatory="是" Length="100">{驾校全称}</SchoolFullName>
        //    <InChargeOf Type="TEXT" Mandatory="是" Length="20">{负责人姓名}</InChargeOf>
        //    <EconomicType Type="TEXT" Mandatory="否" Length="20">{经济类型}</EconomicType>
        //    <Telephone Type="TEXT" Mandatory="否" Length="20">{电话}</Telephone>
        //    <SchoolAddress Type="TEXT" Mandatory="否" Length="100">{经营地址}</SchoolAddress>
        //    <ZipCode Type="TEXT" Mandatory="否" Length="6">{邮编}</ZipCode>
        //</Document>
        protected int SetSchoolInfo(OUDataInfo userInfo, string schoolGuid, XmlNode XmlData)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return -3;

            IDBDataObject InfoData = (_dataDefine as SystemOrgSchool).GetSchoolInfoData(schoolGuid);

            int result = 0;
            InfoData["fSchoolGuid"] = XmlNodeManage.GetSubValue(XmlData, "SchoolGuid");
            InfoData["fSchoolName"] = XmlNodeManage.GetSubValue(XmlData, "SchoolName");
            InfoData["fSchoolFullName"] = XmlNodeManage.GetSubValue(XmlData, "SchoolFullName");
            InfoData["fInChargeOf"] = XmlNodeManage.GetSubValue(XmlData, "InChargeOf");
            InfoData["fEconomicType"] = XmlNodeManage.GetSubValue(XmlData, "EconomicType");
            InfoData["fTelephone"] = XmlNodeManage.GetSubValue(XmlData, "Telephone");
            InfoData["fSchoolAddress"] = XmlNodeManage.GetSubValue(XmlData, "SchoolAddress");
            InfoData["fZipCode"] = XmlNodeManage.GetSubValue(XmlData, "ZipCode");

            result = InfoData.SaveData() ? 0 : 1;
            return result;
        }

        protected XmlNode GetSchoolInfo(OUDataInfo userInfo, string schoolGuid)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return null;

            IDBDataObject InfoData = (_dataDefine as SystemOrgSchool).GetSchoolInfoData(schoolGuid);
            XmlNode xmlNotesInfoData = XmlNodeManage.GetDocumentNode(_taskGuid, schoolGuid, DATATYPE_SCHOOL_INFO);

            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolGuid", "GUID", InfoData["fSchoolGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolName", "TEXT", InfoData["fSchoolName"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolFullName", "TEXT", InfoData["fSchoolFullName"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "InChargeOf", "TEXT", InfoData["fInChargeOf"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "EconomicType", "TEXT", InfoData["fEconomicType"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "Telephone", "TEXT", InfoData["fTelephone"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolAddress", "TEXT", InfoData["fSchoolAddress"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ZipCode", "TEXT", InfoData["fZipCode"]);

            return xmlNotesInfoData;
        }
        #endregion

        protected XmlNode GetSchoolList(OUDataInfo userInfo, XmlNode xmlParam)
        {

            string schoolName = XmlNodeManage.GetSubValue(xmlParam, "SchoolName");
            string queryPageIndex = XmlNodeManage.GetSubValue(xmlParam, "PageIndex");
            string queryPageSize = XmlNodeManage.GetSubValue(xmlParam, "PageSize");

            IDBBigObjectSet schools = (_dataDefine as SystemOrgSchool).GetSchoolList(string.Format("fSchoolName like '%{0}%' or fSchoolFullName like '%{0}%'", schoolName));
            
            Pagination dataPage = new Pagination(schools);
            List<IDBDataObject> schoolList = dataPage.GetPageDataSet(queryPageIndex, queryPageSize);
            queryPageIndex = dataPage.PageIndex.ToString();
            queryPageSize = dataPage.PageSize.ToString();

            XmlNode xmlQueryData = XmlNodeManage.GetDocumentNode(_taskGuid, "", DATATYPE_SCHOOL_QUERY);

            //Add the paging nodes.
            XmlNodeManage.SetSubValue(xmlQueryData, "RecordCount", "LONG", schools.DataCount);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageSize", "LONG", queryPageSize);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageIndex", "LONG", queryPageIndex);

            XmlNode schoolRoot = XmlNodeManage.CreateNode("SchoolList");
            XmlNode subNode;

            foreach (IDBDataObject school in schoolList)
            {
                subNode = XmlNodeManage.CreateNode("SchoolInfo");

                XmlNodeManage.SetSubValue(subNode, "SchoolGuid", "GUID", school["fSchoolGuid"]);
                XmlNodeManage.SetSubValue(subNode, "SchoolName", "TEXT", school["fSchoolName"]);
                XmlNodeManage.SetSubValue(subNode, "SchoolFullName", "TEXT", school["fSchoolFullName"]);
                XmlNodeManage.SetSubValue(subNode, "InChargeOf", "TEXT", school["fInChargeOf"]);
                XmlNodeManage.SetSubValue(subNode, "EconomicType", "TEXT", school["fEconomicType"]);
                XmlNodeManage.SetSubValue(subNode, "Telephone", "TEXT", school["fTelephone"]);
                XmlNodeManage.SetSubValue(subNode, "SchoolAddress", "TEXT", school["fSchoolAddress"]);
                XmlNodeManage.SetSubValue(subNode, "ZipCode", "TEXT", school["fZipCode"]);

                schoolRoot.AppendChild(subNode);
            }
            xmlQueryData.AppendChild(schoolRoot);
            return xmlQueryData;
        }
    }
}
